
function Util() {
    return {
        date: require('locutus/php/datetime/date'),
        time: time,
        visitTree: visitTree,
        hasPerm: hasPerm,
        redirectLogin: redirectLogin
    }

    function time() {
        return parseInt(new Date().getTime() / 1000, 10)
    }

    function visitTree() {
        let resultArr = []

        let getTickMenuId = function (obj) {
            if (undefined == obj || null == obj || !obj instanceof Object) {
                return
            }

            if (obj.checked) {
                resultArr.push(obj.id)
            }

            if (null != obj.childrens && obj.childrens.length > 0 && obj.childrens instanceof Array) {
                for (let child of obj.childrens) {
                    getTickMenuId(child)
                }
            }
        }

        return {
            filter: function (arr) {
                if (!arr instanceof Array) {
                    return false
                }
                resultArr = []
                for (let rootMenu of arr) {
                    getTickMenuId(rootMenu)
                }
                return resultArr
            }
        }
    }

    function hasPerm(name, key, list) {
        let result = false
        for (let i = 0; i < list.length; i++) {
            if (list[i][key] == name) {
                result = true
                break
            }
        }
        return result
    }

    function redirectLogin(msg) {
        if (msg == ("Request method 'GET' not supported")) {
            return true
        }
        return false
    }
}

export default new Util()
